//

#import "NetManager.h"
#import "MBProgressHUD.h"

#define URL_BASE @"http://121.42.195.28/v_upload.php"
@implementation NetManager

/**
 *  上传文件
 *
 */
+ (void)POSTFileWithParams:(NSMutableDictionary *)params withUrl:(NSString *)url header:(NSString *)header timeoutInterval:(CGFloat)timeoutInterval FileNetPath:(NSString *)name withFile:(NSData *)File success:(void (^)(id))success connectionFail:(void (^)(NSError * error))connectionFail animationFlag:(BOOL)animationFlag {
    
    CGRect bounds = [[[UIApplication sharedApplication] delegate] window].bounds;
    UIView *blackView = [[UIView alloc] initWithFrame:bounds];
    blackView.backgroundColor = [UIColor clearColor];
    
    if (animationFlag) {
        [[[[UIApplication sharedApplication] delegate] window] addSubview:blackView];
        [MBProgressHUD showHUDAddedTo:blackView animated:YES];
        //        hud.labelText = @"正在加载...";
    }

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setTimeoutInterval:timeoutInterval];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json", @"application/json",@"text/javascript", nil];

    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a = [dat timeIntervalSince1970]*10000;
    NSString *fileName = [NSString stringWithFormat:@"video-%f.png", a];
    NSLog(@"%@", fileName);
    [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:File name:name fileName:fileName mimeType:@"png"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            
            success(responseObject);
            [blackView removeFromSuperview];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (connectionFail) {
            connectionFail(error);
        }
        [blackView removeFromSuperview];

        NSLog(@"Error: %@", error);
    }];
}

+ (void)POSTDataWithParams:(NSMutableDictionary *)params withUrl:(NSString *)url header:(NSString *)header success:(void (^)(id object))success withAnimation:(BOOL)animationFlag connectionFail:(void (^)(NSError * error))connectionFail {
    
    // * * * * * * * * * * * * * * * * 动 画 * * * * * * * * * * * * * * * * * * *//
    
    CGRect bounds = [[[UIApplication sharedApplication] delegate] window].bounds;
    UIView *blackView = [[UIView alloc] initWithFrame:bounds];
    blackView.backgroundColor = [UIColor clearColor];
    
    if (animationFlag) {
        [[[[UIApplication sharedApplication] delegate] window] addSubview:blackView];
        [MBProgressHUD showHUDAddedTo:blackView animated:YES];
        //        hud.labelText = @"正在加载...";
    }
    
    // * * * * * * * * * * * * * *  请 求 配 置 * * * * * * * * * * * * * * * * *//
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json", @"application/json",@"text/javascript", nil];
    if (header) {
        [manager.requestSerializer setValue:header forHTTPHeaderField:@"accesstoken"];
    }
    
    [manager.requestSerializer setTimeoutInterval:30.0f];
    // * * * * * * * * * * * * * *  配 置 解 码 格 式 * * * * * * * * * * * * * * *//
    {
        //    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
        //    manager.responseSerializer.stringEncoding = enc;
    }
    // * * * * * * * * * * * * * * *  请 求 发 起 * * * * * * * * * * * * * * * * //
    
    [manager GET:[@"http://121.42.195.28" stringByAppendingString:url] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        // * * * * * * * * * * * * * * *  请 求 成 功 * * * * * * * * * * * * * * * * //
        if (success) {

            success(responseObject);
            [blackView removeFromSuperview];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        // * * * * * * * * * * * * * * *  请 求 失 败 移 除 动 画 * * * * * * * * * * * * * * * * //
        if (connectionFail) {
            connectionFail(error);
        }
        [blackView removeFromSuperview];
    }];
    
}

+ (void)POST1DataWithParams:(NSMutableDictionary *)params withUrl:(NSString *)url header:(NSString *)header success:(void (^)(id object))success withAnimation:(BOOL)animationFlag {
    
    // * * * * * * * * * * * * * * * * 动 画 * * * * * * * * * * * * * * * * * * *//
    
    CGRect bounds = [[[UIApplication sharedApplication] delegate] window].bounds;
    UIView *blackView = [[UIView alloc] initWithFrame:bounds];
    blackView.backgroundColor = [UIColor clearColor];
    
    if (animationFlag) {
        [[[[UIApplication sharedApplication] delegate] window] addSubview:blackView];
        [MBProgressHUD showHUDAddedTo:blackView animated:YES];
        //        hud.labelText = @"正在加载...";
    }
    
    // * * * * * * * * * * * * * *  请 求 配 置 * * * * * * * * * * * * * * * * *//
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json", @"application/json",@"text/javascript", nil];
    if (header) {
        [manager.requestSerializer setValue:header forHTTPHeaderField:@"accesstoken"];
    }
    
    [manager.requestSerializer setTimeoutInterval:30.0f];
    // * * * * * * * * * * * * * *  配 置 解 码 格 式 * * * * * * * * * * * * * * *//
    {
        //    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
        //    manager.responseSerializer.stringEncoding = enc;
    }
    // * * * * * * * * * * * * * * *  请 求 发 起 * * * * * * * * * * * * * * * * //
    
    [manager POST:[@"http://121.42.195.28" stringByAppendingString:url] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        // * * * * * * * * * * * * * * *  请 求 成 功 * * * * * * * * * * * * * * * * //
        if (success) {
            
            success(responseObject);
            [blackView removeFromSuperview];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        // * * * * * * * * * * * * * * *  请 求 失 败 移 除 动 画 * * * * * * * * * * * * * * * * //
        
        [blackView removeFromSuperview];
    }];
    
}



@end
